﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.Common;
using OneMile.Entity.Model;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
    [MobileAPI(Code = "MC0331", Name = "用户新增地址")]
    public class UAddNewAddress : IProcessMessage
    {
        private BLLUUserAddress bllUAdr;
        public BLLUUserAddress BllUAdr
        {
            get
            {
                if (bllUAdr == null)
                    bllUAdr = BLLFactory.getInstance().CreateUUserAddressBLL();
                return bllUAdr;
            }
        }


        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {   
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);
                // 获取参数字段（具体请参见文档）
                string AddressName = reqMsg.Body.GetFieldValue("AddressName");
                string UserId=reqMsg.Body.GetFieldValue("UserId");
                string Address=reqMsg.Body.GetFieldValue("Address");
                string Consignee = reqMsg.Body.GetFieldValue("Consignee");
                string Mobile=reqMsg.Body.GetFieldValue("Mobile");
                string Rec_CreateBy=reqMsg.Body.GetFieldValue("Rec_CreateBy");
                string Rec_ModifyBy=reqMsg.Body.GetFieldValue("Rec_ModifyBy");

                if (string.IsNullOrEmpty(AddressName) || string.IsNullOrEmpty(UserId) || string.IsNullOrEmpty(Address) || string.IsNullOrEmpty(Mobile))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                if (BllUAdr.IsAddressExit(AddressName,UserId))
                {
                    rspMsg.Head.RspCode = MessageCode.AddressName_Already_Exit.Code;
                    rspMsg.Head.RspMsg = MessageCode.AddressName_Already_Exit.Msg;

                    return rspMsg;
                }
                else
                {
                    EUserAddress addr = new EUserAddress();
                    addr.AddressName = AddressName;
                    addr.UserAddressId = Guid.NewGuid();
                    addr.UserId = new Guid(UserId);
                    addr.Address = Address;
                    addr.Consignee = Consignee;
                    addr.Mobile = Mobile;
                    addr.Rec_CreateBy = Rec_CreateBy;
                    addr.Rec_ModifyBy = Rec_ModifyBy;
                    addr.Disabled = 0;

                    if (BllUAdr.AddUserAddress(addr))
                    {
                        rspMsg.Head.RspCode = MessageCode.Success.Code;
                        rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                        return rspMsg;
                    }
                    else
                    {
                        rspMsg.Head.RspCode = MessageCode.Server_Error.Code;
                        rspMsg.Head.RspMsg = MessageCode.Server_Error.Msg;

                        return rspMsg;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("UAddNewAddress-" + e);
                throw e;
            }
        #endregion
        }
    }
}